package com.bestcem.xm.ticket.dao.converter;

import com.bestcem.xm.ticket.entity.mongo.TicketTemplate;
import com.bestcem.xm.ticket.entity.mysql.TicketTemplateDO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.annotation.Nullable;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/22 18:34
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TicketTemplateConverter {

    @Nullable
    public static TicketTemplate doToDto(TicketTemplateDO ticketTemplateDO) {
        if (Objects.isNull(ticketTemplateDO)) {
            return null;
        }

        TicketTemplate ticketTemplate = new TicketTemplate();
        ticketTemplate.setId(ticketTemplateDO.getTicketTemplateId());
        ticketTemplate.setOrgId(ticketTemplateDO.getOrgId());
        ticketTemplate.setTicketConfigId(ticketTemplateDO.getTicketConfigId());
        ticketTemplate.setCategory(ticketTemplateDO.getCategory());
        ticketTemplate.setCustomFieldLimit(ticketTemplateDO.getCustomFieldLimit());
        ticketTemplate.setDefaultFields(CommonConverter
                .stringToList(ticketTemplateDO.getDefaultFields(), TicketTemplate.FieldInfo.class));
        ticketTemplate.setCustomFields(CommonConverter
                .stringToList(ticketTemplateDO.getCustomFields(), TicketTemplate.FieldInfo.class));

        CommonConverter.setEntity(ticketTemplate, ticketTemplateDO);
        return ticketTemplate;
    }

    @Nullable
    public static TicketTemplateDO dtoToDo(TicketTemplate ticketTemplate) {
        if (Objects.isNull(ticketTemplate)) {
            return null;
        }

        TicketTemplateDO ticketTemplateDO = new TicketTemplateDO();
        ticketTemplateDO.setTicketTemplateId(ticketTemplate.getId());
        ticketTemplateDO.setOrgId(ticketTemplate.getOrgId());
        ticketTemplateDO.setTicketConfigId(ticketTemplate.getTicketConfigId());
        ticketTemplateDO.setCategory(ticketTemplate.getCategory());
        ticketTemplateDO.setCustomFieldLimit(ticketTemplate.getCustomFieldLimit());
        ticketTemplateDO.setDefaultFields(CommonConverter.listToString(ticketTemplate.getDefaultFields()));
        ticketTemplateDO.setCustomFields(CommonConverter.listToString(ticketTemplate.getCustomFields()));

        CommonConverter.setDo(ticketTemplate, ticketTemplateDO);
        return ticketTemplateDO;
    }
}
